local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local ConnectClass = _G.CommonOHOH.load.Connect();
local runService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local modelList = {}
local PaveGui= _G.ClientOHOH.load.UIManager():CreateUI("BrickGui",require(script.PaveGui))
local EventList = ClientOHOH.load.EventList();
local Client_AudioManager = ClientOHOH.load.Client_AudioManager();
local soundId = "pavePath"
local paveSpeed = 0.1
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local PlayerModule = require(player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    self.player=self.parent
    self:AddConnect()
end

function M.AddConnect(self)
    
    _G.client.GameStateChangeEvent:Connect(function(state)
        if state==1 then
            PaveGui:Show()
            self.SetUI(0)
        end
        if state==2 then
            Controls:Enable()
            PaveGui:Hide()
        end
    end)
end

--砸塔结束调用的方法，action事件
function M.StartPave(self,modelIdList,targetPart,EndCFrame)
    -- 禁用玩家输入
    Controls:Disable()
    self.brickCount = #modelIdList
    self.SetUI(self.brickCount)
    self.targetPart =targetPart
    self:CreateModelInstance(modelIdList)
    self:PavePath()
    self:AddProcess(function()
        self:MovePlayer(EndCFrame)
    end)
end

--移动player到下一个墩子
function M.MovePlayer(self,EndCFrame)
    self.player.character.humanoid:MoveTo(EndCFrame.Position) -- 将玩家移动到指定位置
    self.player.character.humanoid.MoveToFinished:Connect(function(reached)
        if reached then
            Controls:Enable()
            self:AddProcess(function()
                self:Wait(2)
                self:DestoryModel()
            end)
            print("玩家已到达指定位置！")
        end
    end)
end

function M.SetUI(brickCount)
    PaveGui:SetText(brickCount)
end


--创建模型实例存入modelList中
function M.CreateModelInstance(self,list)
    for index, id in pairs(list) do
        -- local model = _G.CommonOHOH:GetPath_Loop(id,game.ReplicatedStorage.Resource)
        local model = _G.CommonOHOH:GetPath_Loop("testPart",game.ReplicatedStorage.Resource)
        modelList[index] =model:Clone()
        modelList[index].Name = self.player.user_id
    end
end

--铺路，让砖块从ui附近生成飞向目标位置
function M.PavePath(self)
    local connections = {}
    for i =#modelList, 1, -1 do
        if i==#modelList then
            local targetCFrame = self.targetPart.ctrl.nextPos.value.WorldCFrame
            self.SetUI(i-1)
            Client_AudioManager:PlayToInst(soundId,modelList[i])
            self:FlyToTargetPos(self:GetInitWorldPosition(),targetCFrame,i,connections)
        end
    end
end


--获取砖块生成的起始世界坐标
function M.GetInitWorldPosition(self)
    local worldPos = PaveGui:GetWorldPosition()
    return worldPos
end

--砖块出现动画
function M.FlyToTargetPos(self,initPos,targetCFrame,index,connections)
    modelList[index].Parent = self.targetPart
    local Size = modelList[index].ctrl.model.value.Size
    local originSize = Vector3.new(Size.X,Size.Y,Size.Z)
    modelList[index].ctrl.model.value.Size = Vector3.new(0,0,0)
    modelList[index].PrimaryPart.CFrame = CFrame.new(initPos)
    modelList[index].ctrl.model.value.Transparency = 1
    modelList[index].Parent = self.targetPart
    local tweenInfo = TweenInfo.new(
        paveSpeed,
        Enum.EasingStyle.Quart,
        Enum.EasingDirection.Out,
        0,
        false,
        0
    )
    
    local tweenPrimaryPartInfo = {
        CFrame = targetCFrame
    }

    local tweenPartInfo = {
        Transparency = 0,
        Size = originSize
    }


    local tweenPrimaryPart = {}
    tweenPrimaryPart[index] = TweenService:Create(modelList[index].PrimaryPart,tweenInfo,tweenPrimaryPartInfo)
    local tweenPart = TweenService:Create(modelList[index].ctrl.model.value,tweenInfo,tweenPartInfo)
    tweenPrimaryPart[index].Completed:Connect(function()
        if index == 1 then
            return
        end
        Client_AudioManager:PlayToInst(soundId,modelList[index+1])
        self.SetUI(index-2)
        local targetCFrame = modelList[index].ctrl.nextPos.value.WorldCFrame
        self:FlyToTargetPos(self:GetInitWorldPosition(),targetCFrame,index-1,connections)
    end)

    tweenPrimaryPart[index]:Play();
    tweenPart:Play();
end


function M.DestoryModel(self)
    for _, value in modelList do
        value:Destroy()
    end
    modelList={}
end




M:RegistEvent(EventList.pave_animation_event,M.StartPave)

function M.dtor(self)
	M.super.dtor(self);
end

return M